package com.jd.ty.service;

import java.util.Date;
import java.util.List;

import com.jd.ty.dao.DictionaryDAO;
import com.jd.ty.dao.constant.QueryConstants;
import com.jd.ty.dao.util.HqlCriterions;
import com.jd.ty.pojo.Dictionary;
import com.jd.ty.pojo.Employee;
import com.jd.ty.pojo.Permission;
import com.jd.ty.pojo.Role;
import com.jd.ty.pojo.User;
import com.jd.ty.service.exception.ErrorCode;
import com.jd.ty.service.exception.ErrorMessage;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.CommonUtils;
import com.jd.ty.util.Pager;
import com.jd.ty.web.filter.EmployeeFilter;

public class DictionaryService {

	private DictionaryDAO dictionaryDAO;

	public void setDictionaryDAO(DictionaryDAO dictionaryDAO) {
		this.dictionaryDAO = dictionaryDAO;
	}

	@SuppressWarnings("unchecked")
	public List<Dictionary> getListByFiter(Pager pager,String filter) {
		return dictionaryDAO.getByQuery(pager,filter,null);
	}
	@SuppressWarnings("unchecked")
	public List<Dictionary> getList(String prantCode) {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Dictionary.PARENT + "." + QueryConstants.Dictionary.CODE, prantCode,
				QueryConstants.EQ);
		return (List<Dictionary>) dictionaryDAO.getByQuery(hc.getHqlString());
	}
	@SuppressWarnings("unchecked")
	public List<Dictionary> getListByParentId(long prantId) {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Dictionary.PARENT + "." + QueryConstants.Dictionary.DICTIONARY_ID, prantId,
				QueryConstants.EQ);
		return (List<Dictionary>) dictionaryDAO.getByQuery(hc.getHqlString());
	}
	@SuppressWarnings("unchecked")
	public List<Dictionary> getRootList() {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Dictionary.PARENT, null,
				QueryConstants.EQ);
		return (List<Dictionary>) dictionaryDAO.getByQuery(hc.getHqlString());
	}
	@SuppressWarnings("unchecked")
	public List<Dictionary> getList(long parentId) {
		HqlCriterions hc = new HqlCriterions();
		hc.and(QueryConstants.Dictionary.PARENT + "." + QueryConstants.Dictionary.DICTIONARY_ID, parentId,
				QueryConstants.EQ);
		List<Dictionary> res = (List<Dictionary>) dictionaryDAO.getByQuery(hc.getHqlString());
		return res;
	}
	private Dictionary loadParent(long id){
		Dictionary dictionary = (Dictionary) dictionaryDAO.getById(id);
		dictionaryDAO.initialize(dictionary.getParentDictionary());
		return dictionary.getParentDictionary();
	}
	public Dictionary load(long id) {
		return (Dictionary) dictionaryDAO.getById(id);
	}
	public void delete(Long id) {
		Dictionary dictionary = (Dictionary) dictionaryDAO.loadById(id);
		if (dictionary != null) {
			dictionaryDAO.delete(dictionary);
		}

	}
	public void add(Long parentId, Dictionary dictionary) throws ServiceException {

		HqlCriterions hc = new HqlCriterions();
		if (parentId != null) {
			hc.and(QueryConstants.Dictionary.PARENT + '.' + QueryConstants.Dictionary.DICTIONARY_ID,
					parentId, QueryConstants.EQ);

		}else{
			hc.and(QueryConstants.Dictionary.PARENT ,
					null, QueryConstants.EQ);
		}
		hc.and(QueryConstants.Dictionary.DICTIONARY_NAME, dictionary.getCode(), QueryConstants.EQ);
		if (dictionaryDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY,
					new String[] { dictionary.getDictionaryName() });
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}
		hc.clear();

		if (parentId != null) {
			hc.and(QueryConstants.Dictionary.PARENT + '.' + QueryConstants.Dictionary.DICTIONARY_ID,
					parentId, QueryConstants.EQ);

		}else{
			hc.and(QueryConstants.Dictionary.PARENT ,
					null, QueryConstants.EQ);
		}
		hc.and(QueryConstants.Dictionary.CODE, dictionary.getCode(), QueryConstants.EQ);
		if (dictionaryDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY,
					new String[] { dictionary.getDictionaryName() });
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}
		if (parentId != null) {
			Dictionary parent = (Dictionary)dictionaryDAO.loadById(parentId);
			dictionary.setParentDictionary(parent);
			dictionary.setLevel(parent.getLevel()+1);
		}else{
			dictionary.setLevel(0);
		}
		dictionaryDAO.saveOrUpdate(dictionary);
	}
	
	public void update(Dictionary dictionary) throws ServiceException {

		HqlCriterions hc = new HqlCriterions();

		Dictionary dic=load(dictionary.getDictionaryId());
		Dictionary parent=dic.getParentDictionary();
		
		if(parent!=null){
			hc.and(QueryConstants.Dictionary.PARENT + '.' + QueryConstants.Dictionary.DICTIONARY_ID,
					parent.getDictionaryId(), QueryConstants.EQ);
		}else{
			hc.and(QueryConstants.Dictionary.PARENT ,
					null, QueryConstants.EQ);
		}
		hc.and( QueryConstants.Dictionary.DICTIONARY_ID,
				dictionary.getDictionaryId(), QueryConstants.NE);
		hc.and(QueryConstants.Dictionary.DICTIONARY_NAME, dictionary.getDictionaryName(), QueryConstants.EQ);
		if (dictionaryDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY,
					new String[] { dictionary.getDictionaryName() });
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}
		hc.clear();

		if(parent!=null){
			hc.and(QueryConstants.Dictionary.PARENT + '.' + QueryConstants.Dictionary.DICTIONARY_ID,
					parent.getDictionaryId(), QueryConstants.EQ);
		}else{
			hc.and(QueryConstants.Dictionary.PARENT ,
					null, QueryConstants.EQ);
		}
		hc.and( QueryConstants.Dictionary.DICTIONARY_ID,
				dictionary.getDictionaryId(), QueryConstants.NE);
		hc.and(QueryConstants.Dictionary.CODE, dictionary.getDictionaryName(), QueryConstants.EQ);
		if (dictionaryDAO.getCountByQuery(hc.getHqlString()) > 0) {
			String errMsg = CommonUtils.formatString(ErrorMessage.DUPLICATION_ENTITY,
					new String[] { dictionary.getDictionaryName() });
			throw new ServiceException(ErrorCode.DUPLICATION_ENTITY, errMsg);
		}
		
	
		dic.setCode(dictionary.getCode());
		dic.setDescription(dictionary.getDescription());
		dic.setDictionaryName(dictionary.getDictionaryName());
		
	}
}
